package com.bjpowernode.appio;

import java.io.*;

public class MyApp7 {


    public static void main(String[] args) {

        //缓存容量
        int bufferSize = 1024 * 1024 * 10;

        //源
        String pathSrc = "d:\\031-CRM活动页面分页实现.wmv";
        //目的地
        String pathDes = "c:\\hzj\\aaa.wmv";

        //目的地判断
        File fileDes = new File(pathDes);
        if (fileDes.exists()) {
            boolean delete = fileDes.delete();
            if (!delete) {
                System.out.println("拷贝失败，文件无法删除");
                return;
            }
        }
        //创建文件
        boolean newFile = false;
        try {
            newFile = fileDes.createNewFile();
            if (!newFile) {
                System.out.println("文件创建失败。。。。。。。");
                return;
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建文件失败。。。。。。。。。IO异常");
            return;
        }

        //开始拷贝文件内容

        try {
            //开始读取源文件内容
            byte[] bs = new byte[bufferSize];
            FileInputStream fileInputStream = new FileInputStream(pathSrc);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream,bufferSize);
            //开始准备连接目的地文件
            FileOutputStream fileOutputStream = new FileOutputStream(pathDes);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream,bufferSize);

            //计数器，记录拷贝了多少字节了
            int js = 0;
            int read = 0;
            while ((read = bufferedInputStream.read(bs, 0, bs.length)) != -1) {
                js += read;
                System.out.println("正在拷贝.............." + js);
                bufferedOutputStream.write(bs, 0, read);
            }

            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            fileOutputStream.close();
            bufferedInputStream.close();
            fileInputStream.close();
            System.out.println("拷贝完毕");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件源读取失败.......文件不存在错误");
            return;
        } catch (IOException e) {
            System.out.println("文件源读取失败.......IO错误");
            return;
        }


    }


}
